using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;

// http://www.codeproject.com/aspnet/MyWebResourceProj.asp?df=100&forumid=265875&exp=0&select=1476020
[assembly: System.Web.UI.WebResource(
	"Pitoshka.Web.Controls.Scripts.CalendarPopup.js"
	, "text/javascript"
	, PerformSubstitution = true)]
namespace Pitoshka.Web.Controls
{
	[DefaultProperty("Date")]
	[ToolboxData("<{0}:Calendar runat=server></{0}:Calendar>")]
	[ValidationProperty("Date")]
	public class Calendar : WebControl
	{
		#region Public properties
		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("select")]
		[Localizable(true)]
		public string Text
		{
			get
			{
				String s = (String)ViewState["Text"];
				return ((s == null) ? "select" : s);
			}
			set
			{
				ViewState["Text"] = value;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public string DatePattern
		{
			get
			{
				String s = (String)ViewState["DatePattern"];
				return ((s == null)
					? System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
					: s);
			}
			set
			{
				ViewState["DatePattern"] = value;
			}
		}

		[Category("Appearance")]
		[DefaultValue("")]
		public DateTime Date
		{
			get
			{
				return ViewState["Date"] != null
					? (DateTime)ViewState["Date"]
					: DateTime.MinValue;
			}
			set
			{
				ViewState["Date"] = value;
			}
		}
		private string DateString
		{
			get
			{

				return Date != DateTime.MinValue
					? Date.ToString()
					: "";
			}

		}

		[Category("Appearance")]
		[DefaultValue("10")]
		public int Size
		{
			get
			{
				return
					(ViewState["Size"] != null)
					? (int)ViewState["Size"]
					: 10;
			}
			set { ViewState["Size"] = value; }
		}
		#endregion Public properties

		#region Private properties
		private string javascriptLocVarName
		{
			get { return this.UniqueID + "_cal"; }
		}
		private string inputName
		{
			get { return this.UniqueID + "_input"; }
		}
		private string divName
		{
			get { return this.UniqueID + "_div"; }
		}
		private string anchorName
		{
			get { return this.UniqueID + "_a"; }
		}
		#endregion Private properties

		protected override void OnPreRender(EventArgs e)
		{
			// Register script include

			string resouseKey = "Pitoshka.Web.Controls.Scripts.CalendarPopup.js";
			string scriptLocation
				= Page.ClientScript.GetWebResourceUrl(this.GetType(), resouseKey);
			Page.ClientScript.RegisterClientScriptInclude(
				this.GetType()
				, resouseKey
				, scriptLocation);
			
			
			Page.ClientScript.RegisterClientScriptBlock(
				this.GetType()
				, "calendarInitialization"
				, @"var " + javascriptLocVarName + @" = new CalendarPopup(""" + divName + @""");"
				, true);



			base.OnPreRender(e);
		}
		protected override void RenderContents(HtmlTextWriter output)
		{
			if (Visible)
			{





				// Define client names and ids



				StringBuilder sb = new StringBuilder();


				sb.AppendFormat(@"<div id=""{0}"" style=""position:absolute;visibility:hidden;background-color:white;layer-background-color:white;""></div>"
					, divName);

				sb.AppendFormat(@"<input type=""text"" name=""{0}"" id=""{0}"" value=""{1}"" size=""{2}"">"
					, inputName
					, DateString
					, Size);


				sb.AppendFormat(@"<a href=""#"" onClick=""{0}.select(document.getElementById('{1}'),'{2}','{3}'); return false;"" name=""{2}"" id=""{2}"">{4}</a>"
					, javascriptLocVarName	  // 0
					, inputName				  // 1
					, anchorName			  // 2
					, this.DatePattern		  // 3
					, this.Text				  // 4
					);

				output.Write(sb.ToString());
			}

			base.RenderContents(output);
		}
	}
}